package cn.tedu.weibo.config;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.stereotype.Component;


@Component
public class CaffeineConfig {
    /**定义缓存大小*/
    private static final int MAXIMUM_SIZE = 1000;
    /**定义Cache对象*/
    private Cache<Object, Object> cache;

    public CaffeineConfig() {
        //初始化Cache对象
        cache = Caffeine.newBuilder()
                .maximumSize(MAXIMUM_SIZE)
                .build();//建造模式(构建复杂对象时候,一般建议使用建造模式)
    }

    /**向缓存添加数据*/
    public void put(Object key, Object value) {
        cache.put(key, value);
    }

    /**从缓存取数据*/
    public Object get(Object key) {
        return cache.getIfPresent(key);
    }

    /**移除缓存数据*/
    public void remove(Object  key) {
        cache.invalidate(key);
    }

}
